/*
 * ConfUnit.hpp
 *
 *  Created on: 2013-1-14
 *      Author: heqiu
 */

#ifndef CONFUNIT_HPP_
#define CONFUNIT_HPP_

#include "mycomm.h"
#include "BuildingUnit.hpp"
#include "AnimalUnit.hpp"

class ConfUnit {
public:
	int32_t load(const char * filename) {
		bool result = m_xml.LoadFile(filename);
		if (result) {
			return 0;
		} else {
			return -1;
		}
	}
	int32_t getWindowX() {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement("window").FirstChildElement(
						"pos_x");
		return atoi(handle.Element()->GetText());
	}
	int32_t getWindowY() {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement("window").FirstChildElement(
						"pos_y");
		return atoi(handle.Element()->GetText());
	}
	float getWindowSize() {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement("window").FirstChildElement(
						"size");
		return atof(handle.Element()->GetText());
	}
	uint32_t getWindowWidth() {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement("window").FirstChildElement(
						"width");
		return strtoul(handle.Element()->GetText(), NULL, 10);
	}
	uint32_t getWindowHeight() {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement("window").FirstChildElement(
						"height");
		return strtoul(handle.Element()->GetText(), NULL, 10);
	}
	uint32_t getWindowBPP() {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement("window").FirstChildElement(
						"bpp");
		return strtoul(handle.Element()->GetText(), NULL, 10);
	}
	string getWindowCaption() {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement("window").FirstChildElement(
						"caption");
		return handle.Element()->GetText();
	}
	uint32_t getMapShort() {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement("map").FirstChildElement(
						"short");
		return strtoul(handle.Element()->GetText(), NULL, 10);
	}
	uint32_t getMapLong() {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement("map").FirstChildElement(
						"long");
		return strtoul(handle.Element()->GetText(), NULL, 10);
	}

	void getBuildingType(map<uint32_t, string> & mapBuildingType) {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement(
						"building-type");
		TiXmlNode* node = 0;
		for (node = handle.ToNode()->FirstChild("node"); node;
				node = node->NextSibling("node")) {
			uint32_t uiType = strtoul(node->FirstChildElement("id")->GetText(),
					NULL, 10);
			string sName = node->FirstChildElement("name")->GetText();
			mapBuildingType[uiType] = sName;
		}
	}
	void getAnimalType(map<uint32_t, string> & mapAnimalType) {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement(
						"animal-type");
		TiXmlNode* node = 0;
		for (node = handle.ToNode()->FirstChild("node"); node;
				node = node->NextSibling("node")) {
			uint32_t uiType = strtoul(node->FirstChildElement("id")->GetText(),
					NULL, 10);
			string sName = node->FirstChildElement("name")->GetText();
			mapAnimalType[uiType] = sName;
		}
	}
	void getBuildings(vector<BuildingUnit> & vctBuilding) {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement(
						"building");
		TiXmlNode* node = 0;
		for (node = handle.ToNode()->FirstChild("node"); node;
				node = node->NextSibling("node")) {
			BuildingUnit stBuildingUnit;
			stBuildingUnit.m_iId = strtoul(
					node->FirstChildElement("id")->GetText(), NULL, 10);
			stBuildingUnit.m_pPos.m_x = strtoul(
					node->FirstChildElement("pos_x")->GetText(), NULL, 10);
			stBuildingUnit.m_pPos.m_y = strtoul(
					node->FirstChildElement("pos_y")->GetText(), NULL, 10);
			stBuildingUnit.m_iType = strtoul(
					node->FirstChildElement("type")->GetText(), NULL, 10);
			stBuildingUnit.m_xLen = strtoul(
					node->FirstChildElement("len_x")->GetText(), NULL, 10);
			stBuildingUnit.m_yLen = strtoul(
					node->FirstChildElement("len_y")->GetText(), NULL, 10);
			vctBuilding.push_back(stBuildingUnit);
		}
	}
	void getAnimals(map<uint32_t, AnimalUnit> & mapAnimal) {
		TiXmlHandle docHandle(&m_xml);
		TiXmlHandle handle =
				docHandle.FirstChildElement("root").FirstChildElement("animal");
		TiXmlNode* node = 0;
		for (node = handle.ToNode()->FirstChild("node"); node;
				node = node->NextSibling("node")) {
			AnimalUnit stAnimalUnit;
			stAnimalUnit.m_iId = strtoul(
					node->FirstChildElement("id")->GetText(), NULL, 10);
			stAnimalUnit.m_pPos.m_x = strtoul(
					node->FirstChildElement("pos_x")->GetText(), NULL, 10);
			stAnimalUnit.m_pPos.m_y = strtoul(
					node->FirstChildElement("pos_y")->GetText(), NULL, 10);
			stAnimalUnit.m_iType = strtoul(
					node->FirstChildElement("type")->GetText(), NULL, 10);
			mapAnimal[stAnimalUnit.m_iId] = stAnimalUnit;
		}
	}

	void getWays(map<uint32_t, set<pair<uint32_t, uint32_t> > > & mapWay) {
		{
			TiXmlHandle docHandle(&m_xml);
			TiXmlHandle handle =
					docHandle.FirstChildElement("root").FirstChildElement(
							"way_horizontal");
			TiXmlNode* node = 0;
			for (node = handle.ToNode()->FirstChild("node"); node;
					node = node->NextSibling("node")) {
				uint32_t y = strtoul(node->FirstChildElement("y")->GetText(),
						NULL, 10);
				uint32_t x_b = strtoul(
						node->FirstChildElement("x_b")->GetText(), NULL, 10);
				uint32_t x_e = strtoul(
						node->FirstChildElement("x_e")->GetText(), NULL, 10);
				uint32_t type = strtoul(
						node->FirstChildElement("type")->GetText(), NULL, 10);
				for (uint32_t ui = x_b; ui <= x_e; ui++) {
					pair<uint32_t, uint32_t> stPair(ui, y);
					mapWay[BuildingUnit::getWayId(type)].insert(stPair);
				}
			}
		}
		{
			TiXmlHandle docHandle(&m_xml);
			TiXmlHandle handle =
					docHandle.FirstChildElement("root").FirstChildElement(
							"way_vertical");
			TiXmlNode* node = 0;
			for (node = handle.ToNode()->FirstChild("node"); node;
					node = node->NextSibling("node")) {
				uint32_t x = strtoul(node->FirstChildElement("x")->GetText(),
						NULL, 10);
				uint32_t y_b = strtoul(
						node->FirstChildElement("y_b")->GetText(), NULL, 10);
				uint32_t y_e = strtoul(
						node->FirstChildElement("y_e")->GetText(), NULL, 10);
				uint32_t type = strtoul(
						node->FirstChildElement("type")->GetText(), NULL, 10);
				for (uint32_t ui = y_b; ui <= y_e; ui++) {
					pair<uint32_t, uint32_t> stPair(x, ui);
					mapWay[BuildingUnit::getWayId(type)].insert(stPair);
				}
			}
		}
	}

	TiXmlDocument m_xml;
};

#endif /* CONFUNIT_HPP_ */
